<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Admin\Item_img;
use zgldh\QiniuStorage\QiniuStorage;
use Input;

class Item_imgController extends Controller
{
    public function add(){
        if(Input::method() == 'POST'){
            $data = Input::all();
            $result = Item_img::insert([
                'item_id'   =>	$data['item_id'],
                'img'       =>	$data['img'],
            ]);
            session(['_url' => null]);
            return $result ? '1' : '0';
        }else{

        }
    }

    public function del(){
        $id = Input::get('id');
        $disk = QiniuStorage::disk('qiniu_item');
        $url = Item_img::where('id',$id) -> value('img');
        if($disk->exists($url) == true){
            //删除上一张图片
            $result = $disk->delete($url);
            if($result == false){
                $data = array(
                    'status'	=>	'0',
                    'message'	=>	'删除原文件失败',
                );
                return response() -> json($data);
            }else{
                $result = Item_img::where('id',$id) -> delete();
                return $result ? '1' : '0';
            }
        }else{
            $result = Item_img::where('id',$id) -> delete();
            return $result ? '1' : '0';
        }

    }
}